// import { Dayjs } from 'dayjs';
// import { inject } from 'vue';
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import { curry } from 'lodash'
function useDayjsHelper() {
    dayjs.extend(duration)

    // const dayjs = inject<dayjs>('dayjs') as Dayjs

    // if (!dayjs) return console.warn('未找到dayjs 注入');

    function getTimeStamp(
        str: string | number | Date | dayjs.Dayjs | null | undefined,
    ): number {
        return dayjs(str).valueOf()
    }

    function getDateRange(type: dayjs.ManipulateType, nums: number) {
        const end = dayjs()

        const start = dayjs().subtract(nums, type)

        return [start.valueOf(), end.valueOf()]
    }

    const curryDateRange = curry(getDateRange)

    const getWeekRange = curryDateRange('week')

    const getYearRange = curryDateRange('year')

    const getMonthRange = curryDateRange('month')

    function getNowStamp(type: 'second' | 'millisecond') {
        if (type == 'second') {
            return dayjs().unix()
        } else if (type == 'millisecond') {
            return dayjs().valueOf()
        }

        return null
    }

    function calcDateSpan(
        startDateString: string,
        endDateString: string,
    ): 'day' | 'month' | 'year' {
        // 转换日期字符串为Date对象
        const startDate = dayjs(startDateString)
        const endDate = dayjs(endDateString)

        // 比较年份和月份
        if (startDate.year() !== endDate.year()) {
            return 'year'
        } else if (startDate.month() !== endDate.month()) {
            return 'month'
        } else {
            // 比较日期部分
            // if (startDate.getDate() === endDate.getDate()) {
            //     return '仅一天'
            // } else {
            //     return '跨日'
            // }

            return 'day'
        }
    }

    return {
        getTimeStamp,
        getDateRange,
        curryDateRange,
        getWeekRange,
        getYearRange,
        getMonthRange,
        getNowStamp,
        calcDateSpan,
    }
}

export default useDayjsHelper
